#include "stdafx.h"
#include "clock.h"
#include "timereminder.h"
#include "connection.h"
#include "qclosewindowfilter.h"

#include <QApplication>
#include <QDir>
#include <QAction>
#include <QMenu>
#include <QDebug>
#include <QSystemTrayIcon>
#include <QMessageBox>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

	if (!QSystemTrayIcon::isSystemTrayAvailable()) 
	{
		QMessageBox::critical(0, QObject::tr("Systray"),
			QObject::tr("I couldn't detect any system tray "
			"on this system."));
		return 1;
	}
	QApplication::setQuitOnLastWindowClosed(false);

	qDebug() << "currentPath : " + QDir::currentPath();
	qDebug() << "currentPath : " + QApplication::applicationDirPath();
	QDir::setCurrent(QApplication::applicationDirPath());
    if(!createConnection()) return 1;

    Clock w;
    w.setFixedSize(580, 360);
    w.hide();

	TimeReminder reminder;
	reminder.hide();

	auto showReminderAction = new QAction(QObject::tr("Remind"), &w);
	QObject::connect(showReminderAction, &QAction::triggered, &reminder, [&] {
		reminder.show();
	});

	auto quitAction = new QAction(QObject::tr("&Quit"), &w);
	QObject::connect(quitAction, &QAction::triggered, &w, [] {
		//do something
		setStopTime();
	});
	QObject::connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);

	auto trayIconMenu = new QMenu(&w);
	trayIconMenu->addAction(showReminderAction);
	trayIconMenu->addAction(quitAction);

	auto trayIcon = new QSystemTrayIcon(&w);
	QObject::connect(trayIcon, &QSystemTrayIcon::activated, &w, &Clock::iconActivated);

	trayIcon->setContextMenu(trayIconMenu);
	trayIcon->setIcon(QIcon(":/images/clock.png"));
	trayIcon->show();

	QCloseWindowFilter filter;
	a.installNativeEventFilter(&filter);
    
    return a.exec();
}
